#include <stdio.h>
#include <unistd.h>

#define Max(a,b) ((a)>(b)?(a):(b))

int longestSubarray(int* nums, int numsSize) {
    int max=0,zeroC=0;
    int j=0;
    for(int i=0; i < numsSize; i++){
        if( nums[i] == 0 ){
            zeroC++;
        }
        while( zeroC > 1 ){
            if( nums[j++]==0 )
                zeroC--;
        }
        max=Max(max,i+1-j);
    }
    return max-1;
}
/*
int longestSubarray(int* nums, int numsSize) {
    int max=0,zeroC=0;    
    int left=0,right=0;
    while ( right < numsSize ) {
        if( zeroC <= 1 ){
            if( nums[right] == 0 )
                zeroC++;
            if( zeroC <= 1 )
                max=Max(max,right-left+1);
            right++;
        }
        else{
            if( 0==nums[left++] )
                zeroC--;
        }
    }
    return Max(0,max-1);
}*/

int main(){
    int a[]={0,1,1,1,0,1,1,0,1};
    //int a[]={1,1,0,1};
    //int a[]={1,1,1};
    //int a[]={1,0,0,0,0};
    printf("%d\n",longestSubarray(a,sizeof(a)/sizeof(a[0])));
    return 0;
}
